package com.road.dao.imp;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.road.dao.ICommentDao;
import com.road.model.Comment;
import com.road.model.News;
import com.road.model.See;
import com.road.service.ICommentService;
import com.road.utils.DruidPlugin;

public class CommentDaoImp implements ICommentDao {
//	两种连接池切换
//	QueryRunner qr=new QueryRunner(DataSourceUtils.getDatasSource());
	QueryRunner qr=new QueryRunner(DruidPlugin.getDataSource());
	@Override
	public int saveComment(String userPhone, String newsId, String content) throws SQLException {
		// TODO Auto-generated method stub
		String sql="insert into d_news_comment (phone,news_id,comment_content,comment_time) values(?,?,?,now())";
		Object[][] params=new Object[1][3];
		params[0][0]=userPhone;
		params[0][1]=newsId;
		params[0][2]=content;
		return qr.batch(sql, params).length;
	}

	@Override
	public List<Comment> getCommentListByNewsId(String news_id) throws SQLException {
		// TODO Auto-generated method stub
		String sql="SELECT a.*,b.head_icon,b.user_name from d_news_comment as a,auther_user as b where a.phone=b.phone and a.news_id="+news_id+" order by comment_time desc";
		
		return qr.query(sql, new BeanListHandler<Comment>(Comment.class));
	}

}
